package cn.golxen.blog.controller;

import cn.golxen.blog.commons.utils.Result;
import cn.golxen.blog.commons.vo.GetDraftListVo;
import cn.golxen.blog.entity.Draft;
import cn.golxen.blog.exception.exceptionClass.AddDraftException;
import cn.golxen.blog.exception.exceptionClass.DeleteDraftException;
import cn.golxen.blog.service.DraftService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping(value = "/draft")
public class DraftController {
    @Autowired
    DraftService draftService;

    @PostMapping(value = "/add")
    public Result addDraft(@RequestBody Draft draft){
        Integer integer=draftService.addDraft(draft);
        if (integer!=0)
            return Result.successResult();
        else
            throw new AddDraftException();
    }

    @GetMapping(value = "/getlist")
    public Result getDraftList(){
        List<GetDraftListVo> draftList=draftService.getDraftList();
        return Result.successResult(draftList);
    }

    @GetMapping(value = "/get/{title}")
    public Result getDraftByDraftId(@PathVariable(value = "title") String title){
        Draft draft=draftService.getDraft(title);
        return Result.successResult(draft);
    }

    @GetMapping(value = "/delete/{title}")
    public Result deleteDraftByDraftId(@PathVariable(value = "title") String title){
        Integer integer=draftService.deleteDraft(title);
        if (integer!=0)
            return Result.successResult();
        else
            throw new DeleteDraftException();
    }
}
